import * as echarts from '../../ec-canvas/echarts';
import * as util from '../../utils/util';
var cid = null
let periodChart = null;
let payChart = null;
let scoreChart = null;
let salesChart = null;
let itemsChart = null;
let compareChart = null;
let pieLable = {
  normal: {
    show: true,
    formatter: '{b} {d}%'
  }
}

function initPeriodData(date){
  let data = null
  let legends = []
  let option = {}
  getApp().requestPost('turnovergroup/get/perioddistributions/canteen/' + cid + '/date/' + date, function (res) {
    data = res.data.data    
    for(let i = 0; i < data.length; i++){
      legends.push(data[i].name)
    }
    if (data.length === 0){
      legends = ['暂无数据']
      data = [{"name":"暂无数据","value":"1"}]
    }
    option = {
      // 关闭动画
      animation: false,
      legend: {
        left: 'left',
        data: legends,
        orient: 'vertical'
      },
      series: [
        {
          type: 'pie',
          label: pieLable,
          radius: '55%',
          center: ['50%', '60%'],
          data: data,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    } 
    periodChart.setOption(option)
  })
}
function initPayData(date){
  let data = null
  let legends = []
  let option = {}
  getApp().requestPost('turnovergroup/get/paymentmethod/canteen/' + cid + '/date/' + date, function (res) {
    // console.log(res.data.data)
    data = res.data.data
    for (let i = 0; i < data.length; i++) {
      legends.push(data[i].name)
    }
    if (data.length === 0) {
      legends = ['暂无数据']
      data = [{ "name": "暂无数据", "value": "1" }]
    }
    option = {
      // 关闭动画
      animation: false,
      legend: {
        left: 'left',
        data: legends,
        orient: 'vertical'
      },
      series: [
        {
          type: 'pie',
          label: pieLable,
          radius: '55%',
          center: ['50%', '60%'],
          data: data,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    payChart.setOption(option)
  })   
}
function initScoreData(date){
  let data = null
  let legends = []
  let option = {}
  getApp().requestPost('turnovergroup/get/comprehensivescore/canteen/' + cid + '/date/' + date, function (res) {
    data = res.data.data
    for (let i = 0; i < data.length; i++) {
      legends.push(data[i].name)
    }
    if (data.length === 0) {
      legends = ['暂无数据']
      data = [{ "name": "暂无数据", "value": "1" }]
    }
    option = {
      // 关闭动画
      animation: false,
      legend: {
        left: 'left',
        data: legends,
        orient: 'vertical'
      },
      series: [
        {
          type: 'pie',
          radius: '55%',
          center: ['50%', '60%'],
          label: pieLable,
          data: data,
          itemStyle: {
            emphasis: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    scoreChart.setOption(option)
  })    
}
function initSalesData(date){
  let data = null
  let option = {}
  getApp().requestPost('turnovergroup/get/turnoverandprofitbysevendays/canteen/' + cid + '/date/' + date, function (res) {
    data = res.data.data   
    option = {
      legend: {
        data: ['营业额', '毛利', '毛利占比']
      },
      xAxis: [
        {
          type: 'category',
          data: data.dateArray,
          axisPointer: {
            type: 'shadow'
          },
          axisLabel: {
            interval: 0,
            rotate: 40
          }
        }
      ],
      yAxis: [
        {
          type: 'value',
          name: '元',
          axisLabel: {
            formatter: '￥{value}'
          }
        },
        {
          type: 'value',
          name: '占比',
          axisLabel: {
            formatter: '{value}%'
          }
        }
      ],
      series: [
        {
          name: '营业额',
          type: 'bar',
          data: data.secondOrdinateArray
        },
        {
          name: '毛利',
          type: 'bar',
          data: data.thirdOrdinateArray
        },
        {
          name: '毛利占比',
          type: 'line',
          yAxisIndex: 1,
          data: data.firstOrdinateArray
        }
      ]
    } 
    salesChart.setOption(option)
  })  
}
function initItemData(date){
  let data = null
  let legends = []
  let option = {}
  getApp().requestPost('turnovergroup/get/foodsalestrend/canteen/' + cid + '/date/' + date, function (res) {
    data = res.data.data
    option = {
      legend: {
        data: data.nameArray
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: data.dateArray,
        axisLabel: {
          interval: 0,
          rotate: 40
        }
      },
      yAxis: {
        type: 'value'
      },
      series: data.series
    }
    itemsChart.setOption(option)
  })  
}
function initCompareData(date){
  let data = null
  let option = {}
  getApp().requestPost('turnovergroup/get/windowcontrast/canteen/' + cid + '/date/' + date, function (res) {
    // console.log(res.data.data)
    data = res.data.data
    option = {
      legend: {
        data: ['营业额', '毛利', '毛利占比']
      },
      xAxis: [
        {
          type: 'category',
          data: data.dateArray,
          axisPointer: {
            type: 'shadow'
          },
          axisLabel: {
            interval: 0,
            rotate: 40
          }
        }
      ],
      yAxis: [
        {
          type: 'value',
          name: '元',
          axisLabel: {
            formatter: '￥{value}'
          }
        },
        {
          type: 'value',
          name: '占比',
          axisLabel: {
            formatter: '{value}%'
          }
        }
      ],
      series: [
        {
          name: '营业额',
          type: 'bar',
          data: data.secondOrdinateArray
        },
        {
          name: '毛利',
          type: 'bar',
          data: data.thirdOrdinateArray
        },
        {
          name: '毛利占比',
          type: 'line',
          yAxisIndex: 1,
          data: data.firstOrdinateArray
        }
      ]
    }
    compareChart.setOption(option)
  })  
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: util.formatTime(new Date()),
    salesTable:{},
    profitTable:{},
    period: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
          periodChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(periodChart);
        // periodChart.setOption(initPeriodData(util.formatTime(new Date())));
        initPeriodData(util.formatTime(new Date()))
        return periodChart;
      }
    },
    pay: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
        payChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(payChart);
        // payChart.setOption(initPayData(util.formatTime(new Date())));
        initPayData(util.formatTime(new Date()))
        return payChart;
      }
    },
    score: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
        scoreChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(scoreChart);
        // scoreChart.setOption(initScoreData(util.formatTime(new Date())));
        initScoreData(util.formatTime(new Date()))
        return scoreChart;
      }
    },
    sales: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
        salesChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(salesChart);
        // salesChart.setOption(initSalesData(util.formatTime(new Date())));
        initSalesData(util.formatTime(new Date()))
        return salesChart;
      }
    },
    items: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
        itemsChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(itemsChart);
        // itemsChart.setOption(initItemData(util.formatTime(new Date())));
        initItemData(util.formatTime(new Date()))
        return itemsChart;
      }
    },
    compare: {
      disableTouch: true,
      onInit: function (canvas, width, height) {
        compareChart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(compareChart);
        // compareChart.setOption();
        initCompareData(util.formatTime(new Date()))
        return compareChart;
      }
    }
  },
  dateChange: function(){
    wx.showLoading({
      title: '加载中',      
    })
    let date = this.data.date
    this.getTableData(date)
    initPeriodData(date)
    initPayData(date)
    initScoreData(date)
    initSalesData(date)
    initItemData(date)
    initCompareData(date)
    wx.hideLoading()
  },
  bindDateChange: function (e) {
    console.log(e.detail.value)    
    this.setData({
      date: e.detail.value, 
    })
  },
  sortData(data){
    if (data.flagDay === 0) {
      data.changeDay = 'equal'
    } else {
      // console.log(data.percentDay, data.percentDay < 0)
      if (data.flagDay < 0) {
        data.changeDay = 'down'
      } else {
        data.changeDay = 'up'
      }
    }
    if (data.flagMonth === 0) {
      data.changeMonth = 'equal'
    } else {
      if (data.flagMonth < 0) {
        data.changeMonth = 'down'
      } else {
        data.changeMonth = 'up'
      }
    }
    if (data.flagWeek === 0) {
      data.changeWeek = 'equal'
    } else {
      if (data.flagWeek < 0) {
        data.changeWeek = 'down'
      } else {
        data.changeWeek = 'up'
      }
    }
    return data
  },
  getTableData: function(){
    let date = this.data.date
    let _self = this
    console.log(getApp().globalData)
    getApp().requestPost('turnovergroup/get/allturnover/canteen/' + cid + '/date/' + date, function(res){
      let data = _self.sortData(res.data.data)
      _self.setData({
        salesTable: data
      })
    })
    getApp().requestPost('turnovergroup/get/allprofit/canteen/' + cid + '/date/' + date, function (res) {
      let data = _self.sortData(res.data.data)
      _self.setData({
        profitTable: data
      })
    })
  },
  // /**
  //  * 生命周期函数--监听页面初次渲染完成
  //  */
  onReady: function () {
    this.getTableData()    
  },
    /**
   * 顶部样式
   */
  onLoad: function (options) {
    cid = getApp().globalData.companyId
    wx.setNavigationBarTitle({
      title: '营业概况'
    });
  }
})
